import { getShopStatusAPI, setShopStatusAPI,userLoginAPI } from '@/api/userAPI'
import { getUserInfo, removeUserInfo, setUserInfo } from '@/utils/auth'

const actions = {
  /* 登录的action */
  async userLoginAction(context, loginForm) {
    const { data } = await userLoginAPI(loginForm)
    context.commit('setUserInfo', data)
  },
  /* 退出登录的action */
  logout(context) {
    context.commit('removeUserInfo')
  },

  /* 获取店铺状态的action */
  async getShopStatusAction(context) {
    const { data } = await getShopStatusAPI()
    return data
  },

  /* 修改店铺状态的action */
  async setShopStatusAction(context, status) {
    await setShopStatusAPI(status)
  }

}

const mutations = {
  /* 设置用户信息的mutation */
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
    setUserInfo(userInfo)
  },
  removeUserInfo(state) {
    removeUserInfo()
    state.userInfo = null
  }
}

const state = {
  userInfo: null || getUserInfo()
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
